﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.Numerics;
using System.Drawing;

namespace Gollie
{
	public static class Calculator
	{
		private static Regex IbanCheck = new Regex("^[A-Z]{2}\\d{2}[A-Z0-9]{1,30}", RegexOptions.Compiled);

		public static string VerifyAccountNumber(string number)
		{
			string uNumber = number.ToUpper().Replace(" ", "");
			bool ok = IbanCheck.IsMatch(uNumber);
			if (!ok)
				return null;

			var rearranged = new StringBuilder(uNumber);

			rearranged.Append(uNumber.Substring(0, 4));
			rearranged.Remove(0, 4);

			BigInteger bi = new BigInteger(0);

			foreach (char c in rearranged.ToString())
			{
				if (char.IsLetter(c))
				{
					bi *= 100;
					bi += (c - 'A' + 10);
				}
				else
				{
					bi *= 10;
					bi += (c - '0');
				}
			}

			ok = (bi % 97) == 1;

			if (!ok)
				return null;

			return uNumber;
		}

		public static Size ResizeWithRatio(Size origSize, Size maxSize)
		{
			Double xRatio = (double)origSize.Width / (double)maxSize.Width;
			Double yRatio = (double)origSize.Height / (double)maxSize.Height;
			Double ratio = Math.Max(xRatio, yRatio);
			int nnx = (int)Math.Floor((double)origSize.Width / ratio);
			int nny = (int)Math.Floor((double)origSize.Height / ratio);

			return new Size(nnx, nny);
		}

		public static int Age(DateTime birthDate)
		{
			DateTime today = DateTime.Today;
			int years = today.Year - birthDate.Year;
			if (birthDate > today.AddYears(-years))
				years--;

			return years;
		}
	}
}